#sink('interactionMods.tex')
#
# One interaction at a time:
# base model (no interactions)

#Same model with Lax & Phillips data subsetted to seven issues under study
lp.7issue.table3.mod6.0 <- glmer(have.liberal.policy ~ rescale.opinion. + rescale.nyt.bypolicygroup.full. +
rescale.EWM.full.+ rescale.Berry.full. + rescale.legislative.share.dem.mean.full. +
rescale.gov.control.mean.dem.full. + rescale.prof.full. +
term.limits.full + dirdem.full +
courtelected.full + Powerful.full.strong.balance +
rescale.turnout.full. + rescale.onepartyness.full. +
(rescale.opinion.|ostate.full)+(rescale.opinion.|issue.full),family=binomial(link='logit'),data=lp.7);summary(lp.7issue.table3.mod6)
p.2<-as.numeric(predict(lp.7issue.table3.mod6,type="response")>0.5)
mean(p.2==lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])
table(p.2,lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])

#Model with updated data
new.table3.mod6.0 <- glmer(liberalpolicy ~ Zopinion + Zsalience + Zpubideolunweighted +
Zgovt_ideol + Zleg_avg + Zgov_avg +
ZSquire2009 + termlimit + initiative + electedcourt + powerfulig +
Zavg_turnout + Zone_party_years + (Zopinion|abbr) + (Zopinion|issue),family=binomial(link='logit'),data=data,subset=nebraska==0);summary(new.table3.mod6)
p.3<-as.numeric(predict(new.table3.mod6,type="response")>0.5)
mean(p.3==data$liberalpolicy[data$nebraska==0])
table(p.3,data$liberalpolicy[data$nebraska==0])

#output to LaTeX
texreg(list(lp.7issue.table3.mod6.0,new.table3.mod6.0),custom.model.names=c('2008','2014'),custom.coef.names=c('Intercept','Opinion','Salience','Voter Liberalism','Govt. Liberalism','Democratic Legislature Perc.','Democratic Governor','Professionalization','Term Limits','Citizen Init.','Elected Court','Powerful Int. Group Balance','Turnout','One-Party Dominance', 'Opinion', 'Salience', 'Voter Liberalism','Govt. Liberalism','Democratic Legislature Perc.','Democratic Governor','Professionalization','Term Limits','Citizen Init.','Elected Court','Powerful Int. Group Balance','Turnout','One-Party Dominance'),stars=c(.1),dcolumn=T,label="responsiveness",caption="\\textbf{Policy Responsiveness Models}",caption.above=T,custom.gof.names=c('AIC','BIC','Log Likelihood','Num.~obs.','Num.~states','Num.~issues','Variance: state intercepts','Variance: state opinion slopes','Covariance in state rand.~eff.','Variance: issue intercepts','Variance: issue opinion slopes','Covariance in issue rand.~eff.','Num.~states','Num.~issues','Variance: state intercepts','Variance: state opinion slopes','Covariance in state rand.~eff.','Variance: issue intercepts','Variance: issue opinion slopes','Covariance in issue rand.~eff.'))


# interaction 1: rescale.nyt.bypolicygroup.full.*rescale.opinion.   /   Zopinion*Zsalience

#Same model with Lax & Phillips data subsetted to seven issues under study
lp.7issue.table3.mod6.1 <- glmer(have.liberal.policy ~ rescale.opinion. + rescale.nyt.bypolicygroup.full. +
rescale.EWM.full.+ rescale.Berry.full. + rescale.legislative.share.dem.mean.full. +
rescale.gov.control.mean.dem.full. + rescale.prof.full. +
term.limits.full + dirdem.full +
courtelected.full + Powerful.full.strong.balance +
rescale.turnout.full. + rescale.onepartyness.full. +
rescale.nyt.bypolicygroup.full.*rescale.opinion. +
(rescale.opinion.|ostate.full)+(rescale.opinion.|issue.full),family=binomial(link='logit'),data=lp.7);summary(lp.7issue.table3.mod6)
p.2<-as.numeric(predict(lp.7issue.table3.mod6,type="response")>0.5)
mean(p.2==lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])
table(p.2,lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])

#Model with updated data
new.table3.mod6.1 <- glmer(liberalpolicy ~ Zopinion + Zsalience + Zpubideolunweighted +
Zgovt_ideol + Zleg_avg + Zgov_avg +
ZSquire2009 + termlimit + initiative + electedcourt + powerfulig +
Zavg_turnout + Zone_party_years +
Zopinion*Zsalience + (Zopinion|abbr) + (Zopinion|issue),family=binomial(link='logit'),data=data,subset=nebraska==0);summary(new.table3.mod6)
p.3<-as.numeric(predict(new.table3.mod6,type="response")>0.5)
mean(p.3==data$liberalpolicy[data$nebraska==0])
table(p.3,data$liberalpolicy[data$nebraska==0])




# interaction 2: rescale.prof.full.*rescale.opinion.  / ZSquire2009*Zopinion

#Same model with Lax & Phillips data subsetted to seven issues under study
lp.7issue.table3.mod6.2 <- glmer(have.liberal.policy ~ rescale.opinion. + rescale.nyt.bypolicygroup.full. +
rescale.EWM.full.+ rescale.Berry.full. + rescale.legislative.share.dem.mean.full. +
rescale.gov.control.mean.dem.full. + rescale.prof.full. +
term.limits.full + dirdem.full +
courtelected.full + Powerful.full.strong.balance +
rescale.turnout.full. + rescale.onepartyness.full. +
rescale.prof.full.*rescale.opinion. +
(rescale.opinion.|ostate.full)+(rescale.opinion.|issue.full),family=binomial(link='logit'),data=lp.7);summary(lp.7issue.table3.mod6)
p.2<-as.numeric(predict(lp.7issue.table3.mod6,type="response")>0.5)
mean(p.2==lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])
table(p.2,lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])

#Model with updated data
new.table3.mod6.2 <- glmer(liberalpolicy ~ Zopinion + Zsalience + Zpubideolunweighted +
Zgovt_ideol + Zleg_avg + Zgov_avg +
ZSquire2009 + termlimit + initiative + electedcourt + powerfulig +
Zavg_turnout + Zone_party_years +
ZSquire2009*Zopinion + (Zopinion|abbr) + (Zopinion|issue),family=binomial(link='logit'),data=data,subset=nebraska==0);summary(new.table3.mod6)
p.3<-as.numeric(predict(new.table3.mod6,type="response")>0.5)
mean(p.3==data$liberalpolicy[data$nebraska==0])
table(p.3,data$liberalpolicy[data$nebraska==0])



# interaction 3: term.limits.full*rescale.opinion.  / termlimit*Zopinion

#Same model with Lax & Phillips data subsetted to seven issues under study
lp.7issue.table3.mod6.3 <- glmer(have.liberal.policy ~ rescale.opinion. + rescale.nyt.bypolicygroup.full. +
rescale.EWM.full.+ rescale.Berry.full. + rescale.legislative.share.dem.mean.full. +
rescale.gov.control.mean.dem.full. + rescale.prof.full. +
term.limits.full + dirdem.full +
courtelected.full + Powerful.full.strong.balance +
rescale.turnout.full. + rescale.onepartyness.full. +
term.limits.full*rescale.opinion. +
(rescale.opinion.|ostate.full)+(rescale.opinion.|issue.full),family=binomial(link='logit'),data=lp.7);summary(lp.7issue.table3.mod6)
p.2<-as.numeric(predict(lp.7issue.table3.mod6,type="response")>0.5)
mean(p.2==lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])
table(p.2,lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])

#Model with updated data
new.table3.mod6.3 <- glmer(liberalpolicy ~ Zopinion + Zsalience + Zpubideolunweighted +
Zgovt_ideol + Zleg_avg + Zgov_avg +
ZSquire2009 + termlimit + initiative + electedcourt + powerfulig +
Zavg_turnout + Zone_party_years +
termlimit*Zopinion + (Zopinion|abbr) + (Zopinion|issue),family=binomial(link='logit'),data=data,subset=nebraska==0);summary(new.table3.mod6)
p.3<-as.numeric(predict(new.table3.mod6,type="response")>0.5)
mean(p.3==data$liberalpolicy[data$nebraska==0])
table(p.3,data$liberalpolicy[data$nebraska==0])




# interaction 4: dirdem.full*rescale.opinion.  / initiative*Zopinion

#Same model with Lax & Phillips data subsetted to seven issues under study
lp.7issue.table3.mod6.4 <- glmer(have.liberal.policy ~ rescale.opinion. + rescale.nyt.bypolicygroup.full. +
rescale.EWM.full.+ rescale.Berry.full. + rescale.legislative.share.dem.mean.full. +
rescale.gov.control.mean.dem.full. + rescale.prof.full. +
term.limits.full + dirdem.full +
courtelected.full + Powerful.full.strong.balance +
rescale.turnout.full. + rescale.onepartyness.full. +
dirdem.full*rescale.opinion. +
(rescale.opinion.|ostate.full)+(rescale.opinion.|issue.full),family=binomial(link='logit'),data=lp.7);summary(lp.7issue.table3.mod6)
p.2<-as.numeric(predict(lp.7issue.table3.mod6,type="response")>0.5)
mean(p.2==lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])
table(p.2,lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])

#Model with updated data
new.table3.mod6.4 <- glmer(liberalpolicy ~ Zopinion + Zsalience + Zpubideolunweighted +
Zgovt_ideol + Zleg_avg + Zgov_avg +
ZSquire2009 + termlimit + initiative + electedcourt + powerfulig +
Zavg_turnout + Zone_party_years +
initiative*Zopinion + (Zopinion|abbr) + (Zopinion|issue),family=binomial(link='logit'),data=data,subset=nebraska==0);summary(new.table3.mod6)
p.3<-as.numeric(predict(new.table3.mod6,type="response")>0.5)
mean(p.3==data$liberalpolicy[data$nebraska==0])
table(p.3,data$liberalpolicy[data$nebraska==0])



# interaction 5: courtelected.full*rescale.opinion.  / electedcourt*Zopinion

#Same model with Lax & Phillips data subsetted to seven issues under study
lp.7issue.table3.mod6.5 <- glmer(have.liberal.policy ~ rescale.opinion. + rescale.nyt.bypolicygroup.full. +
rescale.EWM.full.+ rescale.Berry.full. + rescale.legislative.share.dem.mean.full. +
rescale.gov.control.mean.dem.full. + rescale.prof.full. +
term.limits.full + dirdem.full +
courtelected.full + Powerful.full.strong.balance +
rescale.turnout.full. + rescale.onepartyness.full. +
courtelected.full*rescale.opinion. +
(rescale.opinion.|ostate.full)+(rescale.opinion.|issue.full),family=binomial(link='logit'),data=lp.7);summary(lp.7issue.table3.mod6)
p.2<-as.numeric(predict(lp.7issue.table3.mod6,type="response")>0.5)
mean(p.2==lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])
table(p.2,lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])

#Model with updated data
new.table3.mod6.5 <- glmer(liberalpolicy ~ Zopinion + Zsalience + Zpubideolunweighted +
Zgovt_ideol + Zleg_avg + Zgov_avg +
ZSquire2009 + termlimit + initiative + electedcourt + powerfulig +
Zavg_turnout + Zone_party_years +
electedcourt*Zopinion + (Zopinion|abbr) + (Zopinion|issue),family=binomial(link='logit'),data=data,subset=nebraska==0);summary(new.table3.mod6)
p.3<-as.numeric(predict(new.table3.mod6,type="response")>0.5)
mean(p.3==data$liberalpolicy[data$nebraska==0])
table(p.3,data$liberalpolicy[data$nebraska==0])



# interaction 6: rescale.turnout.full.*rescale.opinion.  / Zavg_turnout*Zopinion

#Same model with Lax & Phillips data subsetted to seven issues under study
lp.7issue.table3.mod6.6 <- glmer(have.liberal.policy ~ rescale.opinion. + rescale.nyt.bypolicygroup.full. +
rescale.EWM.full.+ rescale.Berry.full. + rescale.legislative.share.dem.mean.full. +
rescale.gov.control.mean.dem.full. + rescale.prof.full. +
term.limits.full + dirdem.full +
courtelected.full + Powerful.full.strong.balance +
rescale.turnout.full. + rescale.onepartyness.full. +
rescale.turnout.full.*rescale.opinion. +
(rescale.opinion.|ostate.full)+(rescale.opinion.|issue.full),family=binomial(link='logit'),data=lp.7);summary(lp.7issue.table3.mod6)
p.2<-as.numeric(predict(lp.7issue.table3.mod6,type="response")>0.5)
mean(p.2==lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])
table(p.2,lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])

#Model with updated data
new.table3.mod6.6 <- glmer(liberalpolicy ~ Zopinion + Zsalience + Zpubideolunweighted +
Zgovt_ideol + Zleg_avg + Zgov_avg +
ZSquire2009 + termlimit + initiative + electedcourt + powerfulig +
Zavg_turnout + Zone_party_years +
Zavg_turnout*Zopinion + (Zopinion|abbr) + (Zopinion|issue),family=binomial(link='logit'),data=data,subset=nebraska==0);summary(new.table3.mod6)
p.3<-as.numeric(predict(new.table3.mod6,type="response")>0.5)
mean(p.3==data$liberalpolicy[data$nebraska==0])
table(p.3,data$liberalpolicy[data$nebraska==0])



# interaction 7: rescale.onepartyness.full.*rescale.opinion. / Zone_party_years*Zopinion

#Same model with Lax & Phillips data subsetted to seven issues under study
lp.7issue.table3.mod6.7 <- glmer(have.liberal.policy ~ rescale.opinion. + rescale.nyt.bypolicygroup.full. +
rescale.EWM.full.+ rescale.Berry.full. + rescale.legislative.share.dem.mean.full. +
rescale.gov.control.mean.dem.full. + rescale.prof.full. +
term.limits.full + dirdem.full +
courtelected.full + Powerful.full.strong.balance +
rescale.turnout.full. + rescale.onepartyness.full. +
rescale.onepartyness.full.*rescale.opinion. +
(rescale.opinion.|ostate.full)+(rescale.opinion.|issue.full),family=binomial(link='logit'),data=lp.7);summary(lp.7issue.table3.mod6)
p.2<-as.numeric(predict(lp.7issue.table3.mod6,type="response")>0.5)
mean(p.2==lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])
table(p.2,lp.7$have.liberal.policy[lp.7$ostate.full!="NE"])

#Model with updated data
new.table3.mod6.7 <- glmer(liberalpolicy ~ Zopinion + Zsalience + Zpubideolunweighted +
Zgovt_ideol + Zleg_avg + Zgov_avg +
ZSquire2009 + termlimit + initiative + electedcourt + powerfulig +
Zavg_turnout + Zone_party_years +
Zone_party_years*Zopinion + (Zopinion|abbr) + (Zopinion|issue),family=binomial(link='logit'),data=data,subset=nebraska==0);summary(new.table3.mod6)
p.3<-as.numeric(predict(new.table3.mod6,type="response")>0.5)
mean(p.3==data$liberalpolicy[data$nebraska==0])
table(p.3,data$liberalpolicy[data$nebraska==0])

#output to LaTeX
texreg(list(lp.7issue.table3.mod6.1,new.table3.mod6.1,lp.7issue.table3.mod6.2,new.table3.mod6.2,lp.7issue.table3.mod6.3,new.table3.mod6.3,lp.7issue.table3.mod6.4,new.table3.mod6.4,lp.7issue.table3.mod6.5,new.table3.mod6.5,lp.7issue.table3.mod6.6,new.table3.mod6.6,lp.7issue.table3.mod6.7,new.table3.mod6.7),custom.model.names=c('2008','2014','2008','2014','2008','2014','2008','2014','2008','2014','2008','2014','2008','2014'),custom.coef.names=c('Intercept','Opinion','Salience','Voter Liberalism','Govt. Liberalism','Democratic Legislature Perc.','Democratic Governor','Professionalization','Term Limits','Citizen Init.','Elected Court','Powerful Int. Group Balance','Turnout','One-Party Dominance','Salience$\\times$Op.', 'Opinion', 'Salience', 'Voter Liberalism','Govt. Liberalism','Democratic Legislature Perc.','Democratic Governor','Professionalization','Term Limits','Citizen Init.','Elected Court','Powerful Int. Group Balance','Turnout','One-Party Dominance','Salience$\\times$Op.','Professionalization$\\times$Op.', 'Professionalization$\\times$Op.','Term Limits$\\times$Op.', 'Term Limits$\\times$Op.','Citizen Init.$\\times$Op.', 'Citizen Init.$\\times$Op.','Elected Court$\\times$Op.', 'Elected Court$\\times$Op.','Turnout$\\times$Op.', 'Turnout$\\times$Op.','One-Party Dominance$\\times$Op.','One-Party Dominance$\\times$Op.'),stars=c(.1),dcolumn=T,label="responsiveness",caption="\\textbf{Policy Responsiveness Models}",caption.above=T,custom.gof.names=c('AIC','BIC','Log Likelihood','Num.~obs.','Num.~states','Num.~issues','Variance: state intercepts','Variance: state opinion slopes','Covariance in state rand.~eff.','Variance: issue intercepts','Variance: issue opinion slopes','Covariance in issue rand.~eff.','Num.~states','Num.~issues','Variance: state intercepts','Variance: state opinion slopes','Covariance in state rand.~eff.','Variance: issue intercepts','Variance: issue opinion slopes','Covariance in issue rand.~eff.'))
#sink()